﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineCooperation.DB;
using OnlineCooperation.Entity;

namespace OnlineCooperation
{
    public partial class TeamPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["TeamID"] == null || Session["TeamID"].ToString() == "")
            {
                Response.Write("<script>alert('请先选择一个团队');window.location='TeamIndex.aspx';</script>");
            }
            TeamDB td=new TeamDB();
            if (!IsPostBack)
            {
                Team t = td.GetTeamByID(int.Parse((Session["TeamID"].ToString())));
                Response.Write(
                    @"<script src=""js/jquery.js""></script>
                        <script>
            $(document).ready(function () {
            var tn=document.getElementById(""SetTeamName"");
            tn.value='" + t.Name + @"';
            var tde=document.getElementById(""InputDescription"");
            tde.value='" + t.Description + @"';
});
            </script>");

            }
            else
            {
                      
            }
        }

        protected void ModifyTeamInfo(object sender, EventArgs e)
        {
            TeamDB td = new TeamDB();
            Team t2 = td.GetTeamByID(int.Parse((Session["TeamID"].ToString())));
            string name = Request.Form["SetTeamName"];
            string des = Request.Form["InputDescription"];
            Team t = new Team(t2.ID, name, des, int.Parse(Session["UserID"].ToString()));
            int code = td.Update(t);
            if (code == 0)
                Response.Write(@"<script>alert('团队名已存在，修改失败');</script>");
            else
                Response.Write(@"<script>alert('修改成功');</script>");    
        }

        //删除团队
        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            TeamDB TDB = new TeamDB();
            int teamid = int.Parse(Session["TeamID"].ToString());
            TDB.Delete(teamid);
            Response.Redirect("TeamIndex.aspx");
        }

        //退出团队
        protected void Button_Quit_Click(object sender, EventArgs e)
        {
            UserDB UDB = new UserDB();
            User user = UDB.GetUserByID(int.Parse(Session["UserID"].ToString()));
            TeamDB TDB = new TeamDB();
            Team team = TDB.GetTeamByID(int.Parse(Session["TeamID"].ToString()));
            RelUserTeamDB RUTDB = new RelUserTeamDB();
            if (user.ID == team.AdminID)
                Response.Write(@"<script>alert('您是管理员，不能退出哦o(╯□╰)o');</script>");
            else
            {
                if (RUTDB.remRel(team, user) == 1)
                    Response.Write(@"<script>alert('您已成功退出该团队啦~');</script>");
                else
                    Response.Write(@"<script>alert('出错啦%>_<%');</script>");
            }
        }
    }
}